package org.jeecgframework.web.service.impl;

import org.activiti.engine.TaskService;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.ZyTimeTaskOfProcurementService;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.SendMailUtil;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by red on 2016/10/28.
 */
@Service("zyTimeTaskOfProcurementServiceImpl")
public class ZyTimeTaskOfProcurementServiceImpl extends CommonServiceImpl implements ZyTimeTaskOfProcurementService {

    private static final Logger logger = Logger.getLogger(ZyTimeTaskOfProcurementServiceImpl.class);
    @Autowired
    private SystemService systemService;

    @Override
    public void sendNotHandledProcurementFromWx() {
        logger.info("开始检查昨日零件采购单");
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DAY_OF_MONTH,-1);

        String yesterday = DateUtils.formatDate(c1.getTime(),"yyyy-MM-dd");
        String dateParam = "and create_date between '"+yesterday+"' and '"+yesterday+" 23:59'";

        String sql = "select procument_no as pno from cbs_procurement_note p1 where p1.source=2 and p1.state=0 "
                + dateParam;

        logger.info("查询SQL:"+sql);
        List<String> proNo = findListbySql(sql);
        String needCommitNo = "";
        for(String m : proNo){
            needCommitNo += ","+ m;
        }
        logger.info("未处理订单编号字符串:"+needCommitNo);
        if(needCommitNo.length() > 1){
            List<TSRoleUser> roleUsers = findHql("from TSRoleUser where TSRole.roleCode=?","barcoHDManager");
            for(TSRoleUser tr : roleUsers){
                if(tr.getTSUser() != null && StringUtil.isNotEmpty(tr.getTSUser().getEmail())){
                    logger.info("发送邮件至："+tr.getTSUser().getEmail());
                    ZyEmailEntity me = new ZyEmailEntity();
                    me.setEmailSubject("服务订单超时提醒");
                    me.setEmailReceiver(tr.getTSUser().getEmail());
                    Map param = new HashMap();
                    param.put("procurementNo",needCommitNo.substring(1));
                    param.put("url", StaticValue.HTTP_HEADER);
                    logger.info("发送邮件");
                    SendMailUtil.sendFtlMail(me,"mail_wxpro_tobarcoHdManager.ftl",param,systemService);
                }
            }
        }
    }

}
